<template>
    <div class="container">
    <el-form class="register-form" ref="regForm" :model="regForm" :rules="rules">
      <h1 class="register-title">后台管理系统</h1>
      <router-link to="/login" class="toggleType">去登录</router-link>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="regForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="regForm.password" type="password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="repassword">
        <el-input v-model="regForm.repassword" type="password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" size="middle" style="width: 420px" @click="register">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  name: 'register-index',
  data () {
    const checkRePassword = (rule, value, callback) => {
      if (value !== this.regForm.password) {
        return callback(new Error('两次密码必须一致'))
      } else {
        // 必须返回callback() 否则表单校验失败
        return callback()
      }
    }
    return {
      regForm: {
        username: '',
        password: '',
        repassword: ''
      },
      loading: false,
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 5, max: 12, message: '用户名长度需要在6-12位之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 5, max: 16, message: '密码长度需要在6-12位之间', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { validator: checkRePassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async register () {
      this.loading = true
      await this.$refs.regForm.validate(async isOK => {
        if (isOK) {
          try {
            await register(this.regForm)
            this.$message.success('注册成功')
          } catch (error) {
            console.log(error)
            // 不管结果如何  都执行下面代码
          } finally {
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.container {
  min-height: 1100px;
  width: 100%;
  overflow: hidden;
  // 如果在样式表中使用@别名时，需要存在@前面加上~符号，否则不识别
  background: url("~@/assets/images/loginBg.png") no-repeat;
  background-size: cover;
  background-position: center;
  .register-form {
    margin: 280px auto;
    padding: 30px 60px;
    text-align: center;
    background-color: #fff;
    box-sizing: border-box;
    width: 540px;
    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
    .register-title {
      font-size: 32px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #131523;
      line-height: 44px;
    }
    .toggleType {
      color: #1e5eff;
      text-decoration: none;
    }
  }
}
</style>
